package cc.vicp.djx314.starfood.aspectj.modelrelate;

import java.util.List;

import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cc.vicp.djx314.starfood.domain.dish.Dish;
import cc.vicp.djx314.starfood.domain.dish.ProvideTime;
import cc.vicp.djx314.starfood.domain.dish.SeriesDishProvideTime;
import cc.vicp.djx314.starfood.util.ModelTreatment;

/**
 * 对某些mapper层的方法进行拦截，处理结果
 * @author 水山清风
 *
 */
public class ModelTreatmentInterceptor {
	/**
	 * 输出日志的Log实例化
	 */
	private static final Logger logger = LoggerFactory.getLogger(ModelTreatmentInterceptor.class);

	/**
	 * 处理要用到的util工具类
	 */
	private ModelTreatment modelTreatment;

	/**
	 * 设置处理要用到的util工具类
	 * @return
	 */
	public void setModelTreatment(ModelTreatment modelTreatment) {
		this.modelTreatment = modelTreatment;
	}

	/**
	 * 默认构造函数
	 */
	public ModelTreatmentInterceptor() {
	}

	/**
	 * 对返回的对象是Dish的mapper层的函数进行处理，为其添加seriesDishProvideTimes属性
	 * @param joinPoint
	 * @return
	 * @throws Throwable
	 */
	public Dish setSeriesDishProvideTimesAsObj(ProceedingJoinPoint joinPoint) throws Throwable {
		logger.info("为Dish添加属性");
		Dish dish = (Dish) joinPoint.proceed();
		if (dish != null) {
			List<ProvideTime> provideTimes = dish.getProvideTimes();
			List<SeriesDishProvideTime> seriesDishProvideTimes = modelTreatment
					.setSeriesDishProvideTimes(provideTimes);
			dish.setSeriesDishProvideTimes(seriesDishProvideTimes);
		}
		return dish;
	}

	/**
	 * 对返回的对象是List<Dish>的mapper层的函数进行处理，为结果集里面的每一个元素添加seriesDishProvideTimes属性
	 * @param joinPoint
	 * @return
	 * @throws Throwable
	 */
	public List<Dish> setSeriesDishProvideTimesAsList(ProceedingJoinPoint joinPoint) throws Throwable {
		logger.info("为Dish添加属性");
		@SuppressWarnings("unchecked")
		List<Dish> dishes = (List<Dish>) joinPoint.proceed();
		if (dishes != null && dishes.size() > 0) {
			for (Dish dish : dishes) {
				List<ProvideTime> provideTimes = dish.getProvideTimes();
				List<SeriesDishProvideTime> seriesDishProvideTimes = modelTreatment
						.setSeriesDishProvideTimes(provideTimes);
				dish.setSeriesDishProvideTimes(seriesDishProvideTimes);
			}
		}
		return dishes;
	}

	/**
	 * bean后处理器InitializingBean，检查该类初始化是否成功
	 */
	public void afterPropertiesSet() throws Exception {
		logger.info("创建model层操作拦截类成功！");
	}
}
